/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.domain.Alumni;
import me.zhengjie.modules.anniversary.repository.AlumniRepository;
import me.zhengjie.modules.anniversary.service.AlumniService;
import me.zhengjie.modules.anniversary.service.dto.AlumniDto;
import me.zhengjie.modules.anniversary.service.dto.AlumniQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.AlumniMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2020-07-28
**/
@Service
@RequiredArgsConstructor
public class AlumniServiceImpl implements AlumniService {

    private final AlumniRepository alumniRepository;
    private final AlumniMapper alumniMapper;

    @Override
    public Map<String,Object> queryAll(AlumniQueryCriteria criteria, Pageable pageable){
        Page<Alumni> page = alumniRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(alumniMapper::toDto));
    }

    @Override
    public List<AlumniDto> queryAll(AlumniQueryCriteria criteria){
        return alumniMapper.toDto(alumniRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AlumniDto findById(Long id) {
        Alumni alumni = alumniRepository.findById(id).orElseGet(Alumni::new);
        ValidationUtil.isNull(alumni.getId(),"Alumni","id",id);
        return alumniMapper.toDto(alumni);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AlumniDto create(Alumni resources) {
        return alumniMapper.toDto(alumniRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Alumni resources) {
        Alumni alumni = alumniRepository.findById(resources.getId()).orElseGet(Alumni::new);
        ValidationUtil.isNull( alumni.getId(),"Alumni","id",resources.getId());
        alumni.copy(resources);
        alumniRepository.save(alumni);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            alumniRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AlumniDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AlumniDto alumni : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", alumni.getName());
            map.put("性别", alumni.getGender());
            map.put("出生年月", alumni.getBirthDate());
            map.put("民族ID", alumni.getNationId());
            map.put("政治面貌ID", alumni.getPoliticalId());
            map.put("联系电话", alumni.getPhone());
            map.put("工作单位", alumni.getWorkUnit());
            map.put("任职部门", alumni.getWorkDepartment());
            map.put("职务", alumni.getPositional());
            map.put("职务", alumni.getJob());
            map.put("简介", alumni.getBrief());
            map.put("照片路径", alumni.getPhotoPath());
            map.put("创建者名称", alumni.getCreateBy());
            map.put("创建时间", alumni.getCreateTime());
            map.put("修改者名称", alumni.getUpdatedBy());
            map.put("修改时间", alumni.getUpdateTime());
            map.put("排序", alumni.getSort());
            map.put("备注", alumni.getRemark());
            map.put("是否启用", alumni.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}